<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("relationshipFunction.php");

$errMsg = sxREQUEST("msg");

$objUser = $_SESSION[USEROBJ];
	
if(isset($_REQUEST["reqtype"]) && $_REQUEST["reqtype"] == "save"){
	$errMsg .= relationshipIns();
	$entityPar = sxREQUEST("entityPar");
	$entityChi = sxREQUEST("entityChi");
	$descrParChi = sxREQUEST("descrParChi");
	$descrChiPar = sxREQUEST("descrChiPar");
	$note = sxREQUEST("note");
	$parentOptionality = sxREQUEST("parentOptionality");
	$childrenOptionality = sxREQUEST("childrenOptionality");
	$childrenMulteplicity = sxREQUEST("childrenMulteplicity");
	$integrityCheck = sxREQUEST("integrityCheck");
	$cascadeDelete = sxREQUEST("cascadeDelete");
	$number = sxREQUEST("number");
}else{
	$entityPar = sxREQUEST('ent');
	$entityChi = "";
	$descrParChi = "";
	$descrChiPar = "";
	$note = "";
	$parentOptionality = 'N';
	$childrenOptionality = 'Y';
	$childrenMulteplicity = 'Y';
	$integrityCheck = 'Y';
	$cascadeDelete = 'N';
	$number = 10;
}
//leggo l'elenco delle entity disponibili
$sSQL = "SELECT * FROM entity ORDER BY entity";
$objSDAO->query($sSQL, "sel");
//output HTML
$aOpz = array("kMenu"=>'dam', "isReloadable"=>false);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/ajax.js"></script>  
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		var errMsg = "";
		var objFrm = document.getElementById("objFrm");
		var objMessArea = document.getElementById("messArea");		
		
		var numberAreaMess = document.getElementById("numberAreaMess");
		var entChiAreaMess = document.getElementById("entChiAreaMess");
		var descrChiParAreaMess = document.getElementById("descrChiParAreaMess");
		var descrParChiAreaMess = document.getElementById("descrParChiAreaMess");
		var attributeChiAreaMess = document.getElementById("attributeChiAreaMess");
		var attributeParAreaMess = document.getElementById("attributeParAreaMess");
		//inizializzo le aree messaggi 
		entChiAreaMess.innerHTML = "";
		numberAreaMess.innerHTML = "";
		descrParChiAreaMess.innerHTML = "";
		descrChiParAreaMess.innerHTML = "";
		attributeChiAreaMess.innerHTML = "";
		attributeParAreaMess.innerHTML = "";
				
		var objEntChi = document.getElementById("entityChi");
		if(objEntChi.value == "" || objEntChi.value == 0){
			errMsg += "<?php echo _t("child entity missing")?><br>";
			objEntChi.className = "inpt01";
			objEntChi.focus();
			entChiAreaMess.innerHTML = "<?php echo _t("child entity missing")?>";
		}else{
			objEntChi.className = "inpt01";
		}
		var objNumber = document.getElementById("number");
		if(objNumber.value == "" || objNumber.value == 0){
			errMsg += "<?php echo _t("number missing")?><br>";
			objNumber.className = "inpt01";
			objNumber.focus();
			numberAreaMess.innerHTML = "<?php echo _t("number missing")?>";
		}else{
			objNumber.className = "inpt01";
		}
		var objDescrParChi = document.getElementById("descrParChi");
		if(objDescrParChi.value == ""){
			errMsg += "<?php echo _t("description parent-child missing")?><br>";
			objDescrParChi.className = "inpt01";
			objDescrParChi.focus();
			descrParChiAreaMess.innerHTML = "<?php echo _t("description parent-child missing")?>";
		}else{
			objDescrParChi.className = "inpt01";
		}
		var objDescrChiPar = document.getElementById("descrChiPar");
		if(objDescrChiPar.value == ""){
			errMsg += "<?php echo _t("description child-parent missing")?><br>";
			objDescrChiPar.className = "inpt01";
			objDescrChiPar.focus();
			descrChiParAreaMess.innerHTML = "<?php echo _t("description child-parent missing")?>";
		}else{
			objDescrChiPar.className = "inpt01";
		}
		if(errMsg == ""){
			objFrm.submit();
		}else{
			//objMessArea.innerHTML = errMsg;
		}		
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
//output comandi
$aCmd = array("insRel"=>array("pag"=>"javascript:document.objFrm.submit()",
										"title"=>_t("save new relationship"),
										"text"=>_t("save"),
										"perm"=>"M"),
					"back"=>array("pag"=>$objUser->getHistory(0,false),
										"title"=>_t('cancel operation'),
										"text"=>_t('cancel'),
										"perm"=>"R")
					);
echo(commandsHTML($aCmd,''));	
//form inserimento dati
if($errMsg != ""){
	
}
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"relationshipIns.php\" method=\"POST\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">");
echo("<tr>" .
			"<td class=\"titl01\" colspan=\"2\">"._t("Relationship")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"25%\">"._t("Parent entity")."*</td>" .
			"<td class=\"text01\" width=\"75%\">");
echo("<select class=\"inpt01\" name=\"entityPar\" id=\"entityPar\">");
echo("<option value=\"\">----</option>");
while($row = $objSDAO->fetch("sel")){
	$chk = "";
	if($row["entity"] == $entityPar)
		$chk = "SELECTED";
	echo("<option value=\"".$row["entity"]."\" $chk>".$row["entity"]."</option>");	
}
echo("</select>\n</td></tr>");
echo("<tr><td class=\"labl01\">"._t("Child entity")."*</td>" .
			"<td class=\"text01\">");
echo("<select class=\"inpt01\" name=\"entityChi\" id=\"entityChi\">");
echo("<option value=\"\">----</option>");
$row = $objSDAO->fetch("sel",0);
while($row){
	$chk = "";
	if($row["entity"] == $entityChi)
		$chk = "SELECTED";
	echo("<option value=\"".$row["entity"]."\" $chk>".$row["entity"]."</option>");	
	$row = $objSDAO->fetch("sel");
}
echo("</select>");
echo("</td></tr>\n<tr>" .
			"<td class=\"labl01\">"._t("Number")."*</td>" .
			"<td class=\"text01\">" .
			"<input class=\"inpt01\" type=\"text\" name=\"number\" id=\"number\" maxlength=\"8\" value=\"$number\" size='4'>" .
			"</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Description parent-child")."*</td>" .
			"<td class=\"text01\" colspan=\"1\">" .
			"<input class=\"inpt01\" type=\"text\" name=\"descrParChi\" id=\"descrParChi\" maxlength=\"80\" size=\"80\" value=\"".sxHE($descrParChi)."\">" .
			"&nbsp;&nbsp;<span id=\"descrParChiAreaMess\" class=\"mess01\"></span>" .
			"</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Description child-parent")."*</td>" .
			"<td class=\"text01\" colspan=\"1\">" .
			"<input class=\"inpt01\" type=\"text\" name=\"descrChiPar\" id=\"descrChiPar\" maxlength=\"80\" size=\"80\" value=\"".sxHE($descrChiPar)."\">" .
			"&nbsp;&nbsp;<span id=\"descrChiParAreaMess\" class=\"mess01\"></span>" .
			"</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Note")."</td>" .
			"<td class=\"text01\" colspan=\"1\"><textarea class=\"inpt01\" name=\"note\" id=\"note\" cols=\"80\" rows=\"3\">".sxHE($note)."</textarea></td>" .
		"</tr>\n");
$chkY = ($parentOptionality == 'Y')?"CHECKED":"";
$chkN = ($parentOptionality == 'N')?"CHECKED":"";
echo("<tr>" .
			"<td class=\"labl01\">"._t("Parent optionality")."</td>" .
			"<td class=\"text01\" colspan=\"1\">" .
			"<input value='Y' class=\"text01\" type=\"radio\" name=\"parentOptionality\" id=\"parentOptionality\" $chkY> yes &nbsp;&nbsp;&nbsp;" .
			"<input value='N' class=\"text01\" type=\"radio\" name=\"parentOptionality\" id=\"parentOptionality\" $chkN> no &nbsp;&nbsp;&nbsp;" .
			"</td>" .
		"</tr>\n");
$chkY = ($childrenOptionality == 'Y')?"CHECKED":"";
$chkN = ($childrenOptionality == 'N')?"CHECKED":"";
echo("<tr>" .
			"<td class=\"labl01\">"._t("Children optionality")."</td>" .
			"<td class=\"text01\" colspan=\"1\">" .
			"<input value='Y' class=\"text01\" type=\"radio\" name=\"childrenOptionality\" id=\"childrenOptionality\" $chkY> yes &nbsp;&nbsp;&nbsp;" .
			"<input value='N' class=\"text01\" type=\"radio\" name=\"childrenOptionality\" id=\"childrenOptionality\" $chkN> no &nbsp;&nbsp;&nbsp;" .
			"</td>" .
		"</tr>\n");
$chkY = ($childrenMulteplicity == 'Y')?"CHECKED":"";
$chkN = ($childrenMulteplicity == 'N')?"CHECKED":"";
echo("<tr>" .
			"<td class=\"labl01\">"._t("Children multiplicity")."</td>" .
			"<td class=\"text01\" colspan=\"1\">" .
			"<input value='Y' class=\"text01\" type=\"radio\" name=\"childrenMulteplicity\" id=\"childrenMulteplicity\" $chkY> yes &nbsp;&nbsp;&nbsp;" .
			"<input value='N' class=\"text01\" type=\"radio\" name=\"childrenMulteplicity\" id=\"childrenMulteplicity\" $chkN> no &nbsp;&nbsp;&nbsp;" .
			"</td>" .
		"</tr>\n");
$chkY = ($integrityCheck == 'Y')?"CHECKED":"";
$chkN = ($integrityCheck == 'N')?"CHECKED":"";
echo("<tr>" .
			"<td class=\"labl01\">"._t("Referential integrity")."</td>" .
			"<td class=\"text01\" colspan=\"1\">" .
			"<input value='Y' class=\"text01\" type=\"radio\" name=\"integrityCheck\" id=\"integrityCheck\" $chkY> yes &nbsp;&nbsp;&nbsp;" .
			"<input value='N' class=\"text01\" type=\"radio\" name=\"integrityCheck\" id=\"integrityCheck\" $chkN> no &nbsp;&nbsp;&nbsp;" .
			"</td>" .
		"</tr>\n");
$chkY = ($cascadeDelete == 'Y')?"CHECKED":"";
$chkN = ($cascadeDelete == 'N')?"CHECKED":"";
echo("<tr>" .
			"<td class=\"labl01\">"._t("Cascade delete")."</td>" .
			"<td class=\"text01\" colspan=\"1\">" .
			"<input value='Y' class=\"text01\" type=\"radio\" name=\"cascadeDelete\" id=\"cascadeDelete\" $chkY> yes &nbsp;&nbsp;&nbsp;" .
			"<input value='N' class=\"text01\" type=\"radio\" name=\"cascadeDelete\" id=\"cascadeDelete\" $chkN> no &nbsp;&nbsp;&nbsp;" .
			"</td>" .
		"</tr>\n");
echo("</form>");
echo("</table>");
echo(commandsHTML($aCmd, ''));
echo(footerHTML());
